package com.wffz.comm.utils;

import java.util.Random;

/**
 * @author sunlei
 * @className CodeUtils
 * @Description
 * @date 2022-12-23 11:48
 **/
public class CodeUtils {

    public static String randomCode() {
        Random r = new Random();
        Integer nextInt = r.nextInt(10000);
        return String.valueOf(nextInt);
    }


    public static String getInvitationCode(int length) {

        StringBuffer sbf = new StringBuffer();
        Random random = new Random();

//        int length = 6;//准备生成几位随机数

        //参数length，表示生成几位随机数
        for (int i = 0; i < length; i++) {

            String flag = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(flag)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                sbf.append((char) (random.nextInt(26) + temp));
            } else if ("num".equalsIgnoreCase(flag)) {
                sbf.append(String.valueOf(random.nextInt(10)));
            }
        }
        return sbf.toString();
    }

    public static String getStringRandom(int length) {
        Random random = new Random();
        //把随机生成的数字转成字符串
        String str = String.valueOf(random.nextInt(9));
        for (int i = 1; i < length; i++) {
            str += random.nextInt(9);
        }
        return str;
    }


    public static void main(String[] args) {
        String aa = getStringRandom(4);
        System.out.println(aa);
    }
}
